//---------------------------------------------\\
// Personal Chest Plugin for CraftBukkit Servers \\
//---------------2014 Ryan Harrison----------------\\
package uk.co.vertigomoon.pchest;

import java.util.logging.Level;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;

import uk.co.vertigomoon.pchest.PlayerManager.ChestState;

/**
 * Class to handle all events and commands that the Personal Chest plugin
 * is interested in.
 */
public class EventListener implements Listener, CommandExecutor {
   /**Public constant defining the "chest" command**/
   public static final String CHEST_COMMAND = "chest";
   /**Constant defining the HELP argument**/
   private static final String HELP_ARGUMENT = "HELP";
   /**The parent {@link Plugin} to this {@link EventListener}**/
   private Plugin plugin;
   /**The {@link PlayerManager} to sent chest command requests to **/
   private PlayerManager playerManager;

   /**
    * Constructs a new {@link EventListener}
    */
   public EventListener(Plugin plugin){
      this.plugin = plugin;
      playerManager = new PlayerManager();
   }//End method EventListener

   /**
    * Method to listen to onBlockPlaced events and forward them to the chest handler.
    * @param event the event to handle.
    */
   @EventHandler(priority=EventPriority.HIGHEST)
   public void onBlockPlaced(BlockPlaceEvent event){
      Block block = event.getBlock();
      if(block.getType() == Material.CHEST){
      }//End if
   }//End method onBlockPlaced

   /**
    * Method to listen for {@link PlayerJoinEvent}s and register the connected {@link Player} to
    * the personal chest plugin.
    * @param event the event to handle.
    */
   @EventHandler public void onPlayerJoin(PlayerJoinEvent event){
      Player player = event.getPlayer();
      playerManager.updateState(event.getPlayer(), ChestState.NONE, false);
      player.sendMessage("Registered for personal chest, type /chest help for more information");
   }//End method onPlayerJoin

   /**
    * Method to listen for {@link PlayerQuitEvent}s and unregister the disconnecting {@link Player}
    * from the personal chest plugin
    * @param event the event to handle
    */
   @EventHandler public void onPlayerLeave(PlayerQuitEvent event){
      playerManager.disconnectPlayer(event.getPlayer());
      plugin.getLogger().log(Level.INFO, event.getPlayer().getName() + " has been removed from chest");
   }//End method onPlayerLeave

   /**
    * Method to listen for and handle the "chest" command
    */
   @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
      if(command.getName().equals(CHEST_COMMAND) && sender instanceof Player){
         Player player = (Player)sender;
         plugin.getLogger().log(Level.INFO, "Recieved chest command from player " + player.getName());

         if(args.length != 1) return false; //This plugin only takes 1 argument, any more or less notify the player.

         String argument = args[0].trim().toUpperCase();

         if(argument.equals(HELP_ARGUMENT)){
            player.sendMessage("/chest lock | unlock | none");
            player.sendMessage("Type chest followed by the state you wish to set yourself in");
            player.sendMessage("lock : Any chest you create in this state will be locked to you");
            player.sendMessage("unlock : Any chest locked by you that you access in this state will become unlocked");
            player.sendMessage("none : Any chest interaction from this plugin from you will stop, your chests will remain locked");
            return true;
         } else {
            try{
               ChestState state = ChestState.valueOf(argument);
               playerManager.updateState(player, state, true);
               plugin.getLogger().log(Level.INFO, player.getName() + " updated to state " + state.toString());
               return true;
            } catch (IllegalArgumentException e){
               return false;
            }//End try/catch
         }//End if
      }//End if
      return false;
   }//End method onCommand

}//End class EventListener
